var HttpClient = function() {
    this.get = function(aUrl, aCallback) {
        var anHttpRequest = new XMLHttpRequest();
        anHttpRequest.onreadystatechange = function() {
            if (anHttpRequest.readyState == 4 && anHttpRequest.status == 200)
                aCallback(anHttpRequest.responseText);
        }

        anHttpRequest.open( "GET", aUrl, true );
        anHttpRequest.send( null );
    }
}

var client = new HttpClient();
client.get('http://bufk.antandros.com.tr:9998/gosteriler', function(response) {
    // do something with response
    var gosteri_listesi = JSON.parse(response);
    for (var i = 0; i < gosteri_listesi.length; i++) {
        console.log(gosteri_listesi[i])
        var x = document.getElementById("gosteriler");
        var option = document.createElement("option");
        var tarih = gosteri_listesi[i]["tarih"];
        var saat = gosteri_listesi[i]["saat"];
        var yer = gosteri_listesi[i]["yer"];
        var tarih_pretty = tarih.slice(0,2) + "." + tarih.slice(2,4) + "." + tarih.slice(4,6);
        var saat_pretty = saat.slice(0,2) + ":" + saat.slice(2,4);
        var gosteri = tarih_pretty + " " + saat_pretty + " " + yer;
        option.text = gosteri;
        option.value = tarih;
        x.add(option);
    }
});

var sira = 'ABCDEFGHIJKLMNOPRST';
var x = document.getElementById("sira");
for(var harf in sira) {
    var option = document.createElement("option");
    option.text = sira[harf];
    option.value = sira[harf];
    x.add(option);
}

function numaraYukle(select){
    var selection = document.getElementById("sira");
   if(selection.value==='A'){
       for (var i = 1; i < 21; i++) {
           var x = document.getElementById('numara');
           var option = document.createElement("option");
           option.text = i;
           option.value = i;
           x.add(option);

       }
   } else if(select.value == 'B') {
       for (var i = 1; i < 24; i++) {
           var x = document.getElementById('numara');
           var option = document.createElement("option");
           option.text = i;
           option.value = i;
           x.add(option);
       }
   } else if(select.value == 'C') {
       for (var i = 1; i < 27; i++) {
           var x = document.getElementById('numara');
           var option = document.createElement("option");
           option.text = i;
           option.value = i;
           x.add(option);
       }
   } else if(select.value == 'D') {
       for (var i = 1; i < 27; i++) {
           var x = document.getElementById('numara');
           var option = document.createElement("option");
           option.text = i;
           option.value = i;
           x.add(option);
       }
   }else if(select.value == 'E') {
       for (var i = 1; i < 29; i++) {
           var x = document.getElementById('numara');
           var option = document.createElement("option");
           option.text = i;
           option.value = i;
           x.add(option);
       }
   }else if(select.value == 'F') {
       for (var i = 1; i < 29; i++) {
           var x = document.getElementById('numara');
           var option = document.createElement("option");
           option.text = i;
           option.value = i;
           x.add(option);
       }
   }else if(select.value == 'G') {
       for (var i = 1; i < 29; i++) {
           var x = document.getElementById('numara');
           var option = document.createElement("option");
           option.text = i;
           option.value = i;
           x.add(option);
       }
   }else if(select.value == 'H') {
       for (var i = 1; i < 29; i++) {
           var x = document.getElementById('numara');
           var option = document.createElement("option");
           option.text = i;
           option.value = i;
           x.add(option);
       }
   }else if(select.value == 'I') {
       for (var i = 1; i < 27; i++) {
           var x = document.getElementById('numara');
           var option = document.createElement("option");
           option.text = i;
           option.value = i;
           x.add(option);
       }
   }else if(select.value == 'J') {
       for (var i = 1; i < 27; i++) {
           var x = document.getElementById('numara');
           var option = document.createElement("option");
           option.text = i;
           option.value = i;
           x.add(option);
       }
   }else if(select.value == 'K') {
       for (var i = 1; i < 27; i++) {
           var x = document.getElementById('numara');
           var option = document.createElement("option");
           option.text = i;
           option.value = i;
           x.add(option);
       }
   }else if(select.value == 'L') {
       for (var i = 1; i < 27; i++) {
           var x = document.getElementById('numara');
           var option = document.createElement("option");
           option.text = i;
           option.value = i;
           x.add(option);
       }
   }else if(select.value == 'M') {
       for (var i = 1; i < 27; i++) {
           var x = document.getElementById('numara');
           var option = document.createElement("option");
           option.text = i;
           option.value = i;
           x.add(option);
       }
   }else if(select.value == 'N') {
       for (var i = 1; i < 27; i++) {
           var x = document.getElementById('numara');
           var option = document.createElement("option");
           option.text = i;
           option.value = i;
           x.add(option);
       }
   }else if(select.value == 'O') {
       for (var i = 1; i < 27; i++) {
           var x = document.getElementById('numara');
           var option = document.createElement("option");
           option.text = i;
           option.value = i;
           x.add(option);
       }
   }else if(select.value == 'P') {
       for (var i = 1; i < 27; i++) {
           var x = document.getElementById('numara');
           var option = document.createElement("option");
           option.text = i;
           option.value = i;
           x.add(option);
       }
   }else if(select.value == 'R') {
       for (var i = 1; i < 27; i++) {
           var x = document.getElementById('numara');
           var option = document.createElement("option");
           option.text = i;
           option.value = i;
           x.add(option);
       }
   }else if(select.value == 'S') {
       for (var i = 1; i < 27; i++) {
           var x = document.getElementById('numara');
           var option = document.createElement("option");
           option.text = i;
           option.value = i;
           x.add(option);
       }
   }else if(select.value == 'T') {
       for (var i = 1; i < 21; i++) {
           var x = document.getElementById('numara');
           var option = document.createElement("option");
           option.text = i;
           option.value = i;
           x.add(option);
       }
   }  else{
    console.log("error");
   }

}

function satisYap() {
    var values = {};
    var x = document.getElementById("form");
    var inputs = x.elements;
    var gosteri = inputs[0].value;
    var sira = inputs[1].value;
    var numara = inputs[2].value;
    var kategori = inputs[3].value;
    var isim = inputs[4].value;
    var eposta = inputs[5].value;
    var telefon = inputs[6].value;
    var query = "/satis/" + gosteri + "/" + sira.toLowerCase() + numara + "/" + kategori + "/" + isim + "/" + eposta + "/" + telefon;
    var client = new HttpClient();
    client.get('http://bufk.antandros.com.tr:9998' + query, function(response) {
        document.getElementById("uyari").innerHTML = response;
    });
}

function rezerveEt() {
    var values = {};
    var x = document.getElementById("form");
    var inputs = x.elements;
    var gosteri = inputs[0].value;
    var sira = inputs[1].value;
    var numara = inputs[2].value;
    var kategori = inputs[3].value;
    var isim = inputs[4].value;
    var eposta = inputs[5].value;
    var telefon = inputs[6].value;
    var query = "/rezervasyon/" + gosteri + "/" + sira.toLowerCase() + numara + "/" + kategori + "/" + isim + "/" + eposta + "/" + telefon;
    var client = new HttpClient();
    client.get('http://bufk.antandros.com.tr:9998' + query, function(response) {
        document.getElementById("uyari").innerHTML = response;
    });

}

function satilanlar() {
    var donus = "";
    var gosteri_tarihi = document.getElementById("gosteriler").value;
    console.log(gosteri_tarihi);
   var client = new HttpClient();
client.get('http://bufk.antandros.com.tr:9998/satilanlar/' + gosteri_tarihi, function(response) {
    // do something with response
var satilan_biletler = JSON.parse(response);
console.log(satilan_biletler);
  var col = [];
  for (var i = 0; i < satilan_biletler.length; i++) {
    for (var key in satilan_biletler[i]) {
      if (col.indexOf(key) === -1) {
        col.push(key);
      }
    }
  }

  var table = document.createElement("table");
  table.className = "table table-striped table-sm"

  var tr = table.insertRow(-1);                   // TABLE ROW.

  for (var i = 0; i < col.length; i++) {
    var th = document.createElement("th");      // TABLE HEADER.
    th.innerHTML = col[i];
    tr.appendChild(th);
  }

  for (var i = 0; i < satilan_biletler.length; i++) {

    tr = table.insertRow(-1);

    for (var j = 0; j < col.length; j++) {
      var tabCell = tr.insertCell(-1);
      tabCell.innerHTML = satilan_biletler[i][col[j]];
    }
  }
      var divContainer = document.getElementById("showData");
  divContainer.innerHTML = "";
  divContainer.appendChild(table);
 }
);
    console.log(donus);


}

function rezervasyonlar() {
    var donus = "";
    var gosteri_tarihi = document.getElementById("gosteriler").value;
    console.log(gosteri_tarihi);
   var client = new HttpClient();
client.get('http://bufk.antandros.com.tr:9998/rezervasyonlar/' + gosteri_tarihi, function(response) {
    // do something with response
var rezerve_biletler = JSON.parse(response);
console.log(rezerve_biletler);
  var col = [];
  for (var i = 0; i < rezerve_biletler.length; i++) {
    for (var key in rezerve_biletler[i]) {
      if (col.indexOf(key) === -1) {
        col.push(key);
      }
    }
  }

  var table = document.createElement("table");
  table.className = "table table-striped table-sm"

  var tr = table.insertRow(-1);                   // TABLE ROW.

  for (var i = 0; i < col.length; i++) {
    var th = document.createElement("th");      // TABLE HEADER.
    th.innerHTML = col[i];
    tr.appendChild(th);
  }

  for (var i = 0; i < rezerve_biletler.length; i++) {

    tr = table.insertRow(-1);

    for (var j = 0; j < col.length; j++) {
      var tabCell = tr.insertCell(-1);
      tabCell.innerHTML = rezerve_biletler[i][col[j]];
    }
  }
      var divContainer = document.getElementById("showData");
  divContainer.innerHTML = "";
  divContainer.appendChild(table);
 }
);
    console.log(donus);


}